#include "StdThread.h"
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
struct StdThread
{
    pthread_t threadid;
};

thread *InitThread(void *(*func)(void *), void *arg)
{
    thread *t=(thread *)malloc(sizeof(thread));
    if(t==NULL)
    {
        printf("Initthread malloc error\n");
        return NULL;
    }
    if(pthread_create(&t->threadid,NULL,func,arg)==0)
    {
        return t;
    }
    else
    {
        printf("create pthread error\n");
        return NULL;
    }
}


unsigned long int GetThreadId(thread *t)
{
    return t->threadid;
}

void CloseThread(thread *t)
{
    if(pthread_cancel(t->threadid)==0)
    {
        free(t);
        return ;
    }
    else
    {
        printf("close thread error\n");
    }
}

void *JoinThread(thread *t)
{
    void *value;
    pthread_join(t->threadid,&value);
    return value;
}

void DetachThread(thread *t)
{
    if(pthread_detach(t->threadid))
    {
        printf("DetachThread error\n");
    }
}
